package com.boarsoft.message.bean;

import java.util.Set;

import com.boarsoft.message.broker.RecipientFinder;
import com.boarsoft.message.store.MessageRecover;

/**
 * 实现TargetQueue，添加消息时会持久化（如果有Roller的话
 * 
 * @author Mac_J
 *
 */
public class TargetMQ extends MessageQueue {
	/** 队列 */
	public static final short TYPE_QUEUE = 0;
	/** 主题 */
	public static final short TYPE_TOPIC = 1;
	/** 动态收件人消息队列 */
	public static final short TYPE_DYNAMIC = 2;
	/** 用于接收签收消息的队列 */
	public static final short TYPE_ACKNOWLEDGE = 3;

	/** 不持久化，本地发送（是否在本地入队取决于队列容量是否为0） */
	public static final short RA_MEM = 0;
	/** 不持久化，由兄弟节点（异地）发送（不在本地入队，是否在异地入队取决于队列容量是否为0） */
	// public static final short RA_MEM_COPY = 1;
	/** 先同步持久化，再本地发送（是否在本地入队取决于队列容量是否为0） */
	public static final short RA_SYNC = 10;
	/** 事务型消息 */
	public static final short RA_TRAN = 20;
	/** 先同步持久化，再由兄弟节点（异地）发送（不在本地入队，是否在异地入队取决于队列容量是否为0） */
	public static final short RA_SYNC_MEM = 11;

	/** 收件人查找器 */
	protected RecipientFinder recipientFinder;
	/** 消息类型 */
	protected short type = TYPE_QUEUE;
	/** 队列可靠性 */
	protected short reliability = RA_MEM;
	/** 是否由消息服务器代为发送 */
	protected boolean relay = false;
	/** 消息有效期，单位分钟 */
	protected int term = 3;
	/** */
	protected Set<String> recipients;
	/** 队列数据恢复器 */
	protected MessageRecover recover;

	protected boolean order;
	
	public TargetQueueInfo getQueueInfo() {
		TargetQueueInfo r = new TargetQueueInfo();
		r.setCapacity(capacity);
		r.setCatalog(catalog);
		r.setCode(code);
		r.setName(name);
		r.setPriority(priority);
		if (roller != null) {
			r.setRoller(roller.toString());
		}
		r.setType(type);
		r.setReliability(reliability);
		r.setRelay(relay);
		r.setTerm(term);
		if (recipientFinder != null) {
			r.setRecipientFinder(recipientFinder.toString());
		}
		return r;
	}

	public void setRecover(MessageRecover recover) {
		if (recover == null) {
			return;
		}
		this.recover = recover;
		recover.setRoller(roller);
		recover.setQueue(this);
		recover.start();
	}

	// ------------------

	public MessageRecover getRecover() {
		return recover;
	}

	public short getType() {
		return type;
	}

	public void setType(short type) {
		this.type = type;
	}

	public short getReliability() {
		return reliability;
	}

	public void setReliability(short reliability) {
		this.reliability = reliability;
	}

	public boolean isRelay() {
		return relay;
	}

	public void setRelay(boolean relay) {
		this.relay = relay;
	}

	public int getTerm() {
		return term;
	}

	public void setTerm(int term) {
		this.term = term;
	}

	public RecipientFinder getRecipientFinder() {
		return recipientFinder;
	}

	public void setRecipientFinder(RecipientFinder recipientFinder) {
		this.recipientFinder = recipientFinder;
	}

	public Set<String> getRecipients() {
		return recipients;
	}

	public void setRecipients(Set<String> recipients) {
		this.recipients = recipients;
	}

	public boolean isOrder() {
		return order;
	}

	public void setOrder(boolean order) {
		this.order = order;
	}
}